package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize ,String name){
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        lqw.orderByDesc(Dish::getUpdateTime);
        Page<Dish> page1 = dishService.page(pageInfo, lqw);
        //对象拷贝，除了records
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
        //获取records
        List<Dish> records = pageInfo.getRecords();
        //遍历records
        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
//            将item对象拷贝到dishDto
            BeanUtils.copyProperties(item, dishDto);
//          分类id
            Long categoryId = item.getCategoryId();
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);
//            如果category不为空，取出菜品名称
            if (null != category) {
                String categoryName = category.getName();
//                把菜品名称设回DishDto
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    /**
     * 修改菜品，数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto byIdWithFlavor = dishService.getByIdWithFlavor(id);
        return R.success(byIdWithFlavor);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改信息保存成功");
    }
   /* @GetMapping("list")
    public R<List<Dish>> list(@RequestParam(value = "categoryId") Long id){
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null,Dish::getCategoryId,id);
        lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        lqw.eq(Dish::getStatus,1);
        List<Dish> list = dishService.list(lqw);
        return R.success(list);
    }*/
   @GetMapping("list")
   public R<List<DishDto>> list(Dish dish){
       LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
       lqw.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
       lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
       lqw.eq(Dish::getStatus,1);
       List<Dish> list = dishService.list(lqw);
       List<DishDto> dishDtos = new ArrayList<>();
       //遍历菜品列表，并查询菜品的分类信息及菜品的口味列表
       for (Dish item : list) {
           DishDto dishDto = new DishDto();
           BeanUtils.copyProperties(item,dishDto);
           Long categoryId = item.getCategoryId();
           Category category = categoryService.getById(categoryId);
           if (category!=null){
               String categoryName = category.getName();
               dishDto.setCategoryName(categoryName);
           }
           Long dishId = item.getId();
           LambdaQueryWrapper<DishFlavor> dlqw = new LambdaQueryWrapper<>();
           dlqw.eq(DishFlavor::getDishId,dishId);
           List<DishFlavor> dishFlavorList = dishFlavorService.list(dlqw);
           dishDto.setFlavors(dishFlavorList);
           dishDtos.add(dishDto);
       }
       //
       //C. 组装数据DishDto，并返回
       return R.success(dishDtos);
   }
    @PostMapping("/status/0")
    public R<String> stop(@RequestParam List<Long> ids){
        dishService.stop(ids);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> deleted(@RequestParam List<Long> ids){
        dishService.deleted(ids);
        return R.success("删除成功");
    }
}
